package Forms;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import net.miginfocom.swing.MigLayout;

public class LoginPanel extends JPanel {
	private JTextField userNameField;
	private JPasswordField passwordField;
	private AdminFrame adminParent;
	private MainForm mainParent;
	private String parentName;

	/**
	 * Create the panel.
	 * Simple authentication portal for the mainform and the adminframe
	 */
	public LoginPanel(JFrame parent, String _parentName) {
		parentName = _parentName;

		JButton btnRegister = new JButton("Register");

		JPanel buttonPanel = new JPanel();
		JPanel logPanel = new JPanel();

		switch (parentName) {

		case "main":
			mainParent = (MainForm) parent;
			buttonPanel.add(btnRegister);
			btnRegister.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					//if you come from the mainform and you dont have an account, a passenger can register here
					if (mainParent.isDatabaseConnected()) {
						new RegistrationFrame(mainParent);
					} else {
						JOptionPane.showMessageDialog(getParent(),
								"No Database connected!");
					}
				}
			});
			break;

		case "admin":
			adminParent = (AdminFrame) parent;
			break;
		}
		setBackground(Color.LIGHT_GRAY);
		setBounds(0, 0, 800, 580);
		setLayout(new MigLayout("", "[center][800px]", "[429px]"));
		logPanel.setBackground(Color.GRAY);
		add(logPanel, "flowx,cell 1 0,alignx center,aligny center");
		logPanel.setLayout(new BoxLayout(logPanel, BoxLayout.Y_AXIS));

		JPanel headerPanel = new JPanel();
		logPanel.add(headerPanel);

		JLabel lblLogin = new JLabel("Login");
		headerPanel.add(lblLogin);
		lblLogin.setHorizontalAlignment(SwingConstants.LEFT);

		JPanel userNamePanel = new JPanel();
		logPanel.add(userNamePanel);

		JLabel lblUsername = new JLabel("Username");
		userNamePanel.add(lblUsername);
		lblUsername.setHorizontalAlignment(SwingConstants.CENTER);

		userNameField = new JTextField();
		userNameField.setCaretPosition(0);
		userNamePanel.add(userNameField);
		userNameField.setHorizontalAlignment(SwingConstants.LEFT);
		userNameField.setColumns(10);

		JPanel passwordPanel = new JPanel();
		logPanel.add(passwordPanel);

		JLabel lblPassword = new JLabel("Password");
		passwordPanel.add(lblPassword);

		passwordField = new JPasswordField();
		passwordField.setColumns(10);
		passwordPanel.add(passwordField);

		logPanel.add(buttonPanel);

		JButton btnLogin = new JButton("Login");
		buttonPanel.add(btnLogin);
		btnLogin.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub

				switch (parentName) {
				case "main":
					if (mainParent.logOn(userNameField.getText(), new String(
							passwordField.getPassword()))) {
						passwordField.setText("");
						userNameField.setText("");
					}
					break;

				case "admin":
					adminParent.logOn(userNameField.getText(), new String(
							passwordField.getPassword()));
					break;
				}

			}

		});

	}

}
